package com.yunxiang.summer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.yunxiang.summer.activity.MainActivity;
import com.yunxiang.summer.R;

import java.util.List;

/**
 * 外层适配器：处理日期分组
 */
public class DynamicGroupAdapter extends RecyclerView.Adapter<DynamicGroupAdapter.GroupViewHolder> {
    private Context context;
    private List<MainActivity.DynamicGroup> groupList;

    public DynamicGroupAdapter(Context context, List<MainActivity.DynamicGroup> groupList) {
        this.context = context;
        this.groupList = groupList;
    }

    @NonNull
    @Override
    public GroupViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.item_dynamic_group, parent, false);
        return new GroupViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GroupViewHolder holder, int position) {
        MainActivity.DynamicGroup group = groupList.get(position);
        holder.tvDate.setText(group.getDate());
        
        // 设置内层动态列表
        holder.itemRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        holder.itemRecyclerView.setAdapter(new DynamicItemAdapter(context, group.getDynamics()));
    }

    @Override
    public int getItemCount() {
        return groupList.size();
    }

    public static class GroupViewHolder extends RecyclerView.ViewHolder {
        TextView tvDate;
        RecyclerView itemRecyclerView;

        public GroupViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDate = itemView.findViewById(R.id.tv_date);
            itemRecyclerView = itemView.findViewById(R.id.rv_dynamic_items);
        }
    }
}
